﻿#region License
// Copyright 2010 Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using MediaWiki.MediaWikiConverter.Exporter;
using System.IO;

namespace MediaWiki.MediaWikiConverter.Tests.Exporter
{
  public class MediaWikiExporterTests : TestsBase
  {
    private const string AssetsDirectoryPath = @"..\..\..\..\Assets\";

    [Test]
    public void Export_TextFormatting_Success()
    {
      MemoryStream ms = new MemoryStream();

      using (Stream source = File.OpenRead(AssetsDirectoryPath + @"TextFormatting.docx"))
      {
        MediaWikiExporter exporter = new MediaWikiExporter(source, ms);
        exporter.Export();
      }

      ms.Seek(0, SeekOrigin.Begin);

      StreamReader reader = new StreamReader(ms);
      string content = reader.ReadToEnd();

      Console.WriteLine(content);
    }

    [Test]
    public void Export_ListFormatting_Success()
    {
      MemoryStream ms = new MemoryStream();

      using (Stream source = File.OpenRead(AssetsDirectoryPath + @"ListFormatting.docx"))
      {
        MediaWikiExporter exporter = new MediaWikiExporter(source, ms);
        exporter.Export();
      }

      ms.Seek(0, SeekOrigin.Begin);

      StreamReader reader = new StreamReader(ms);
      string content = reader.ReadToEnd();

      #region Expected
      string expected =
        @"'''Multilevel With Additional Indentation List'''

* First
** First First
**:* First First First
** First Second

'''Mixed Multilevel List'''

# First
#* First First
#*# First First First
#*# First First Second
#* First Second
# Second
# Third
## Third First
##* Third First First
# Forth

'''Numbered List'''

# First
# Second
# Third

'''Bulleted List'''

* First
* Second
* Third

'''Multilevel List'''

# First
## First First
### First First First
### First First Second
## First Second
# Second
# Third
## Third First
### Third First First
# Forth

";
      #endregion

      Assert.AreEqual(expected, content);
    }

    [Test]
    public void Export_TableFormatting_Success()
    {
      MemoryStream ms = new MemoryStream();

      using (Stream source = File.OpenRead(AssetsDirectoryPath + @"TableFormatting.docx"))
      {
        MediaWikiExporter exporter = new MediaWikiExporter(source, ms);
        exporter.Export();
      }

      ms.Seek(0, SeekOrigin.Begin);

      StreamReader reader = new StreamReader(ms);
      string content = reader.ReadToEnd();

      Assert.AreEqual(@"{| class=""prettytable""
|-
|
Top left

|
Top center

|
Top right

|-
|
Middle left

|
Middle center

|
Middle right

|-
|
Bottom left

|
Bottom center

|
Bottom right

|}


", content);
    }

    [Test]
    public void Export_ConcurrentBoldRuns_Success()
    {
      MemoryStream ms = new MemoryStream();

      using (Stream source = File.OpenRead(AssetsDirectoryPath + @"ConcurrentBoldRuns.docx"))
      {
        MediaWikiExporter exporter = new MediaWikiExporter(source, ms);
        exporter.Export();
      }

      ms.Seek(0, SeekOrigin.Begin);

      StreamReader reader = new StreamReader(ms);
      string content = reader.ReadToEnd();

      string expected =
@"'''This is an awesome ‘bold’ piece of text. Which should ''hopefully'' have many runs inside it.'''

";

      Assert.AreEqual(expected, content);
    }

    [Test]
    public void Export_Alignment_Success()
    {
      MemoryStream ms = new MemoryStream();

      using (Stream source = File.OpenRead(AssetsDirectoryPath + @"Alignment.docx"))
      {
        MediaWikiExporter exporter = new MediaWikiExporter(source, ms);
        exporter.Export();
      }

      ms.Seek(0, SeekOrigin.Begin);

      StreamReader reader = new StreamReader(ms);
      string content = reader.ReadToEnd();

      string expected =
@"Left aligned

<center>Center aligned</center>

<div align=""right"">Right aligned</div>

Justified justified justified justified justified justified justified justified justified justified justified justified justified justified justified justified justified justified justified

";

      Assert.AreEqual(expected, content);
    }

    [Test]
    public void Export_Indenting_Success()
    {
      MemoryStream ms = new MemoryStream();

      using (Stream source = File.OpenRead(AssetsDirectoryPath + @"ParagraphIndenting.docx"))
      {
        MediaWikiExporter exporter = new MediaWikiExporter(source, ms);
        exporter.Export();
      }

      ms.Seek(0, SeekOrigin.Begin);

      StreamReader reader = new StreamReader(ms);
      string content = reader.ReadToEnd();

      string expected =
@"'''Test document with tabs and indenting'''

This is a paragraph that starts with a tab. This is a paragraph that starts with a tab. This is a paragraph that starts with a tab. This is a paragraph that starts with a tab. This is a paragraph that starts with a tab. This is a paragraph that starts with a tab. This is a paragraph that starts with a tab. This is a paragraph that starts with a tab.

This is a paragraph that is indented. This is a paragraph that is indented. This is a paragraph that is indented. This is a paragraph that is indented. This is a paragraph that is indented. This is a paragraph that is indented. This is a paragraph that is indented.

Normal paragraph.

";

      Assert.AreEqual(expected, content);
    }

    [Test]
    public void Export_NonBreakingSpaces_Success()
    {
      MemoryStream ms = new MemoryStream();

      using (Stream source = File.OpenRead(AssetsDirectoryPath + @"NonBreakingSpaces.docx"))
      {
        MediaWikiExporter exporter = new MediaWikiExporter(source, ms);
        exporter.Export();
      }

      ms.Seek(0, SeekOrigin.Begin);

      StreamReader reader = new StreamReader(ms);
      string content = reader.ReadToEnd();

      string expected =
@"'''Non-breaking Spaces'''

Normal spaces between here     and here.

Non-breaking spaces between here&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and here.

Mix of spaces between here &nbsp; and here.

";

      Assert.AreEqual(expected, content);
    }

    [Test]
    public void Export_Headings_Success()
    {
      MemoryStream ms = new MemoryStream();

      using (Stream source = File.OpenRead(AssetsDirectoryPath + @"Headings.docx"))
      {
        MediaWikiExporter exporter = new MediaWikiExporter(source, ms);
        exporter.Export();
      }

      ms.Seek(0, SeekOrigin.Begin);

      StreamReader reader = new StreamReader(ms);
      string content = reader.ReadToEnd();

      string expected =
@"'''Headings'''

=Heading 1=

==Heading 2==

===Heading 3===

====Heading 4====

=====Heading 5=====

======Heading 6======

======Heading 7======

Title

''Subtitle''

The end

";

      Assert.AreEqual(expected, content);
    }

    [Test]
    public void Export_TableCellSpanning_Success()
    {
      MemoryStream ms = new MemoryStream();

      using (Stream source = File.OpenRead(AssetsDirectoryPath + @"TableCellSpanning.docx"))
      {
        MediaWikiExporter exporter = new MediaWikiExporter(source, ms);
        exporter.Export();
      }

      ms.Seek(0, SeekOrigin.Begin);

      StreamReader reader = new StreamReader(ms);
      string content = reader.ReadToEnd();

      string expected =
@"{| class=""prettytable""
|-
|
Top left

| colspan=""2"" |
Top center

Top right

|-
|
Middle left

|
Middle center

|
Middle right

|-
| colspan=""2"" |
Bottom left

Bottom center

|
Bottom right

|}


";

      Assert.AreEqual(expected, content);
    }

    [Test]
    public void Export_TextEscaping_Success()
    {
      MemoryStream ms = new MemoryStream();

      using (Stream source = File.OpenRead(AssetsDirectoryPath + @"TextEscaping.docx"))
      {
        MediaWikiExporter exporter = new MediaWikiExporter(source, ms);
        exporter.Export();
      }

      ms.Seek(0, SeekOrigin.Begin);

      StreamReader reader = new StreamReader(ms);
      string content = reader.ReadToEnd();

      string expected =
@"'''Text escaping'''

<nowiki>* Bullet point 1</nowiki>

<nowiki>* Bullet point 2</nowiki>

&lt;span&gt;&lt;nowiki&gt;Text&lt;/nowiki&gt;&lt;/span&gt;

<nowiki>No text ''formatting'' at any '''''point''''' in this paragraph.</nowiki>

'''No '''* bullet point

";

      Assert.AreEqual(expected, content);
    }

    [Test]
    public void Export_ImageCaption_Success()
    {
      MemoryStream ms = new MemoryStream();

      using (Stream source = File.OpenRead(AssetsDirectoryPath + @"ImageCaption.docx"))
      {
        MediaWikiExporter exporter = new MediaWikiExporter(source, ms);
        exporter.Export();
      }

      ms.Seek(0, SeekOrigin.Begin);

      StreamReader reader = new StreamReader(ms);
      string content = reader.ReadToEnd();

      string expected =
@"'''Image Caption'''

[[image:250px-Office2007Logo.png|333x100px]]

'''Figure ''''''1'''

";

      Assert.AreEqual(expected, content);
    }

    [Test]
    public void Export_EncodingBOM_Success()
    {
      byte[] bom = Encoding.UTF8.GetPreamble();

      MemoryStream ms = new MemoryStream();

      using (Stream source = File.OpenRead(AssetsDirectoryPath + @"ImageCaption.docx"))
      {
        MediaWikiExporter exporter = new MediaWikiExporter(source, ms);
        exporter.Export();
      }

      byte[] output = ms.ToArray();

      for (int i = 0; i < bom.Length; i++)
      {
        Assert.AreEqual(bom[i], output[i]);
      }

      ms = new MemoryStream();
      Encoding encoding = new UTF8Encoding(false);

      using (Stream source = File.OpenRead(AssetsDirectoryPath + @"ImageCaption.docx"))
      {
        MediaWikiExporter exporter = new MediaWikiExporter(source, ms);
        exporter.OutputEncoding = encoding;
        exporter.Export();
      }

      output = ms.ToArray();

      for (int i = 0; i < bom.Length; i++)
      {
        Assert.AreNotEqual(bom[i], output[i]);
      }
    }

    [Test]
    public void Export_Links_Success()
    {
      MemoryStream ms = new MemoryStream();

      using (Stream source = File.OpenRead(AssetsDirectoryPath + @"Links.docx"))
      {
        MediaWikiExporter exporter = new MediaWikiExporter(source, ms);
        exporter.Export();
      }

      ms.Seek(0, SeekOrigin.Begin);

      StreamReader reader = new StreamReader(ms);
      string content = reader.ReadToEnd();

      string expected =
@"'''Links'''

[http://www.microsoft.com/ This is a webpage link.]

[mailto:billg@microsoft.com This is an email link.]

[[image:Example.jpg|128x85px|link=http://www.silverlight.net/]]

[http://office.microsoft.com/en-us/default.aspx This is a text link with an image ][[image:Crystal_Clear_app_display.png|link=http://office.microsoft.com/en-us/default.aspx]][http://office.microsoft.com/en-us/default.aspx  in the middle.]

[http://en.wikipedia.org/wiki/The_Office_(U.S._TV_series) This is a ''text'' '''link''' with '''''formatting'''''.]

* [http://www.mediawiki.org/wiki/Help:Links Bullet point link]

=[http://science.slashdot.org/story/10/02/02/2027250/The-Lancet-Recants-Study-Linking-Autism-To-Vaccine?art_pos=1 Heading 1 link]=

[http://www.msdn.com/ This is a text link with an image at the end: ][[image:Crystal_Clear_app_display.png|41x41px|link=http://www.msdn.com/]]



";

      Assert.AreEqual(expected, content);
    }

    [Test]
    public void Export_CustomBulletPoints_Success()
    {
      MemoryStream ms = new MemoryStream();

      using (Stream source = File.OpenRead(AssetsDirectoryPath + @"CustomBulletPoints.docx"))
      {
        MediaWikiExporter exporter = new MediaWikiExporter(source, ms);
        exporter.Export();
      }

      ms.Seek(0, SeekOrigin.Begin);

      StreamReader reader = new StreamReader(ms);
      string content = reader.ReadToEnd();

      string expected = @"'''Certified Competencies'''

We are a Microsoft Gold Certified Partner with the following certified competencies:

* Custom Development Solutions
* Application Infrastructure Development
** Web Development
* Information Worker Solutions
* Microsoft Business Solutions
* Microsoft Dynamics NAV
* Microsoft Dynamics CRM
* Mobility Solutions
** Business Process and Integration Solutions



";

      Assert.AreEqual(expected, content);
    }

    [Test]
    public void Export_HeadingAfterTable_Success()
    {
      MemoryStream ms = new MemoryStream();

      using (Stream source = File.OpenRead(AssetsDirectoryPath + @"HeadingAfterTable.docx"))
      {
        MediaWikiExporter exporter = new MediaWikiExporter(source, ms);
        exporter.Export();
      }

      ms.Seek(0, SeekOrigin.Begin);

      StreamReader reader = new StreamReader(ms);
      string content = reader.ReadToEnd();

      string expected = @"{| class=""prettytable""
|-
|
1

|
2

|-
|
3

|
4

|}
=Heading!=

";

      Assert.AreEqual(expected, content);
    }

    [Test]
    public void Export_UrlLinkEscaping_Success()
    {
      MemoryStream ms = new MemoryStream();

      using (Stream source = File.OpenRead(AssetsDirectoryPath + @"UrlLinkEscaping.docx"))
      {
        MediaWikiExporter exporter = new MediaWikiExporter(source, ms);
        exporter.Export();
      }

      ms.Seek(0, SeekOrigin.Begin);

      StreamReader reader = new StreamReader(ms);
      string content = reader.ReadToEnd();

      Assert.AreEqual(@"'''Url Link Escaping'''

This is not a link – <nowiki>http://www.bing.com</nowiki> – but this is – [http://www.msdn.com/ http://www.msdn.com]

The end.

", content);
    }
  }
}
